
export default function(_){
  // 当前界面-轮询
  // task [{name:名称,time:周期,callback:回调,area:影响页面（默认当前地址）}]
  function Timeout(){
    this.task = {};
    this.valid = (item)=> {
      if(item.area !== "global" && item.area != window.location.pathname){// 限制当前页面
        return false;
      }
      return !!this.task[item.name];
    }
  }

  Timeout.prototype.reset = function(){
    this.task = {};
  }

  Timeout.prototype.exe = function(item){
    clearTimeout(item.timer);
    if(!this.valid(item)){
      return;
    }
    //
    item.callback();
    item.timer = setTimeout(()=>this.exe(item),item.time)
  }

  Timeout.prototype.del = function(name){
    let item = this.task[name];
    if(item){
      clearTimeout(item.timer);
      delete this.task[name];
    }
  }

  Timeout.prototype.add = function(item){
    this.task[item.name] = item;
    this.exe(item);
  }

  // name,callback
  Timeout.prototype.set = function(name,callback,time = 22000){
    this.del(name);
    if(callback){
      this.add({name,callback,time,area:window.location.pathname})
    }
  }
  Timeout.prototype.setGlobal = function(name,callback,time = 22000){
    this.del(name);
    if(callback){
      this.add({name,callback,time,area:"global"})
    }
  }

  _.timeout = new Timeout();

}
